package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {
    private static final String prefix = "business/statementItem/";

    @Resource
    private IStatementService statementService;

    @Resource
    private IStatementItemService statementItemService;

    @Resource
    private IUserService userService;

    // 列表
    @RequiresPermissions("business:statementItem:view")
    @RequestMapping("/itemDetail")
    public String itemDetail(@ModelAttribute("statementId") Long id, Model model) {
        Statement statement = statementService.queryById(id);
        boolean b = statement.getStatus() == 0;
        model.addAttribute("statement", statement);
        if (!b) statement.setPayee(userService.get(statement.getPayeeId()));
        return b ? prefix + "editDetail" : prefix + "showDetail";
    }

    // 支付
    @RequiresPermissions("business:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(@ModelAttribute("statementId") Long id) {
        statementService.payStatement(id);
        return AjaxResult.success();
    }

    // 分页
    @RequiresPermissions("business:statementItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(QueryObject qo, Long statementId) {
        return statementItemService.query(qo, statementId);
    }

    // 保存
    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.save(items);
        return AjaxResult.success();
    }

}
